import os
import math
import argparse

import torch
import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler
from torch.utils.tensorboard.writer import SummaryWriter
from torchvision import transforms

from dataset import MyDataSet
from model import make_vit
from utils import read_split_data, train_one_epoch, evaluate


def main(args):
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    if os.path.exists("./weights") is False:
        os.makedirs("./weights")

    tb_writer = SummaryWriter('./logs')

    train_images_path, train_images_label, val_images_path, val_images_label = read_split_data(
        args.path)

    data_transform = {
        "train":
        transforms.Compose([
            transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
        ]),
        "val":
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
        ])
    }

    # 实例化训练数据集
    train_dataset = MyDataSet(images_path=train_images_path,
                              images_class=train_images_label,
                              transform=data_transform["train"])

    # 实例化验证数据集
    val_dataset = MyDataSet(images_path=val_images_path,
                            images_class=val_images_label,
                            transform=data_transform["val"])

    batch_size = args.bs
    # number of workers / set to 0 on macos
    # nw = min([os.cpu_count(), batch_size if batch_size > 1 else 0, 8])
    # print('Using {} dataloader workers every process'.format(nw))
    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        batch_size=batch_size,
        shuffle=True,
        pin_memory=True,
        num_workers=0,
        collate_fn=train_dataset.collate_fn)

    val_loader = torch.utils.data.DataLoader(val_dataset,
                                             batch_size=batch_size,
                                             shuffle=False,
                                             pin_memory=True,
                                             num_workers=0,
                                             collate_fn=val_dataset.collate_fn)

    model = make_vit(num_classes=args.cls).to(device)

    # init_img = torch.zeros((1, 3, 224, 224), device=device)
    # tb_writer.add_graph(model, init_img)

    if args.wt != "":
        assert os.path.exists(args.wt), "weights file: '{}' not exist.".format(
            args.wt)
        weights_dict = torch.load(args.wt, map_location=device)
        # 删除不需要的权重
        del_wt = []
        for key in weights_dict.keys():
            if 'head' in key:  # or 'attn.qkv.bias' in key:
                del_wt.append(key)
        for k in del_wt:
            del weights_dict[k]
        # 打印权重加载情况
        # print(model.load_state_dict(weights_dict, strict=False))
        model.load_state_dict(weights_dict, strict=False)

    if args.fz:
        for name, para in model.named_parameters():
            # 除 head, pre_logits 外，其他权重全部冻结
            if "head" not in name:
                para.requires_grad_(False)
            else:
                print("training {}".format(name))

    pg = [p for p in model.parameters() if p.requires_grad]
    optimizer = optim.SGD(pg, lr=args.lr, momentum=0.9, weight_decay=5E-5)

    # 余弦学习率衰减
    def lf(x):
        return ((1 + math.cos(x * math.pi / args.epc)) /
                2) * (1 - args.lrf) + args.lrf  # cosine

    scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf)

    for epoch in range(args.epc):
        # train
        train_loss, train_acc = train_one_epoch(model=model,
                                                optimizer=optimizer,
                                                data_loader=train_loader,
                                                device=device,
                                                epoch=epoch)

        scheduler.step()

        # validate
        val_loss, val_acc = evaluate(model=model,
                                     data_loader=val_loader,
                                     device=device,
                                     epoch=epoch)

        tags = [
            "train_loss", "train_acc", "val_loss", "val_acc", "learning_rate"
        ]
        tb_writer.add_scalar(tags[0], train_loss, epoch)
        tb_writer.add_scalar(tags[1], train_acc, epoch)
        tb_writer.add_scalar(tags[2], val_loss, epoch)
        tb_writer.add_scalar(tags[3], val_acc, epoch)
        tb_writer.add_scalar(tags[4], optimizer.param_groups[0]["lr"], epoch)

    torch.save(model.state_dict(), "./weights/model.pth")


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--cls', type=int, default=4)
    parser.add_argument('--epc', type=int, default=50)
    parser.add_argument('--bs', type=int, default=8)
    parser.add_argument('--lr', type=float, default=0.001)
    parser.add_argument('--lrf', type=float, default=0.01)
    # 数据集所在根目录
    parser.add_argument('--path',
                        type=str,
                        default="./aoteman",
                        help='set dataset location')
    # 预训练权重路径，如果不想载入就设置为空字符
    parser.add_argument('--wt',
                        type=str,
                        default='./vit_base_patch16_224.pth',
                        help='initial weights path')
    # 是否冻结权重（除了预测头）
    parser.add_argument('--fz',
                        type=bool,
                        default=True,
                        help='freeze transformer layers')

    opt = parser.parse_args()

    main(opt)
